<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/23
 * Time: 11:33
 */

namespace AppBundle\Task;


class AlertPrice implements TaskInterface
{

    private $container = null;

    public function __construct($container)
    {
        $this->container = $container;
    }

    public function run($options = array())
    {
        $result = $this->process($options);
        if (empty($result)) {
            return null;
        }

        $view_string = $this->container->get('twig')->render('HuobiKLine1min/mail.html.twig', [
            'title' => '价格警报通知',
            'tick' => $options,
            'data' => $result,
        ]);
        // todo: send mail.-> class TaskMail(todo:)
    }

    public function process($options = array())
    {
        $coin = isset($options['coin']) ? $options['coin'] : 0;

        $em = $this->container->get('doctrine')->getManager();
        $huobiAlertPrices = $em->getRepository('AdminBundle:HuobiAlertPrice')->findBy(['coin' => $coin, 'status' => 0]);

        if ($huobiAlertPrices) {
            foreach ($huobiAlertPrices as $huobiAlertPrice) {
                if ($huobiAlertPrice->getDirect() == 'up') {
                    if ($options['close'] > $huobiAlertPrice->getPrice() || $options['high'] > $huobiAlertPrice->getPrice()) {
                        return $huobiAlertPrices;
                    }
                } else {
                    if ($options['close'] < $huobiAlertPrice->getPrice() || $options['low'] < $huobiAlertPrice->getPrice()) {
                        return $huobiAlertPrices;
                    }
                }
            }
        }
        return null;
    }


}